package com.example.administrator.myapplication.Model;

import android.util.Log;

import com.example.administrator.myapplication.Bean.AjaxResult;
import com.example.administrator.myapplication.Bean.Label;
import com.example.administrator.myapplication.Http.Api;
import com.example.administrator.myapplication.Http.RetrofitHelper;
import com.example.administrator.myapplication.Presenter.ILabelListener;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2019/10/14 0014.
 */

public class LabelModel implements ILabelModel{
    @Override
    public void addLabel(Label label, final ILabelListener iLabelListener) {
        RetrofitHelper retrofitHelper = new RetrofitHelper(Api.Server_Host);
        retrofitHelper.addLabel(label).enqueue(new Callback<AjaxResult>() {
            @Override
            public void onResponse(Call<AjaxResult> call, Response<AjaxResult> response) {
                iLabelListener.success(response.body());
            }

            @Override
            public void onFailure(Call<AjaxResult> call, Throwable t) {
                iLabelListener.error(t.toString());
            }
        });
    }

    @Override
    public void getLabels(final ILabelListener iLabelListener) {
        RetrofitHelper retrofitHelper = new RetrofitHelper(Api.Server_Host);
        retrofitHelper.getLabels().enqueue(new Callback<List<HashMap>>() {
            @Override
            public void onResponse(Call<List<HashMap>> call, Response<List<HashMap>> response) {
                iLabelListener.labelList(response.body());
            }

            @Override
            public void onFailure(Call<List<HashMap>> call, Throwable t) {
                iLabelListener.error(t.toString());
            }
        });
    }
}
